package com.ktg.mes.chanjet.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chanjet.openapi.sdk.java.exception.ChanjetApiException;
import com.ktg.common.core.controller.BaseController;
import com.ktg.common.core.domain.Result;
import com.ktg.common.core.domain.TreeSelect;
import com.ktg.common.utils.StringUtils;
import com.ktg.mes.chanjet.entity.TpItemType;
import com.ktg.mes.chanjet.response.InventoryClassResponse;
import com.ktg.mes.chanjet.service.TpItemTypeService;
import com.ktg.mes.chanjet.spi.ChanjetSpi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@RestController
@Slf4j
@RequestMapping("/tplus/itemType")
public class TplusItemTypeController extends BaseController {

    @Autowired
    TpItemTypeService tpItemTypeService;


    @GetMapping("/list")
    public Result<Object> list(@RequestParam(name="itemTypeName",required = false) String itemTypeName){
        QueryWrapper<TpItemType> wrapper = new QueryWrapper<>();
        List<TpItemType> list = new ArrayList<>();
        if(StringUtils.isNotEmpty(itemTypeName)){
           QueryWrapper<TpItemType> wrapper2 = new QueryWrapper<>();
            wrapper2.select("id");
            wrapper2.like("item_type_name",itemTypeName);
            List<Object> ids = tpItemTypeService.listObjs(wrapper2);
            if(ids.size() == 0) return Result.OK(list);
            wrapper.select("*");
            wrapper.in("parent_type_id",ids).or().in("id",ids);
            list = tpItemTypeService.list(wrapper);
        }else{
            wrapper.select("*");
        }
            list = tpItemTypeService.list(wrapper);
        return Result.OK(list);
    }

    @GetMapping("/treeselect")
    public Result<Object> treeList(TpItemType tpItemType){
        List<TreeSelect> tpItemTypes = tpItemTypeService.selectItemTypeList(tpItemType);
        return Result.OK(tpItemTypes);
    }
}
